<template>
	<div class="system-company-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="450px" :close-on-click-modal="false">
			<el-form ref="companyDialogFormRef" :model="state.ruleForm" size="default" label-width="100px">
				<el-row :gutter="35">
					<el-col :xs="24" class="mb20">
						<el-form-item label="答题时间">
							<el-time-picker
								v-model="state.qizhitimes"
								is-range
								range-separator="-"
								start-placeholder="开始时间"
								end-placeholder="结束时间"
								format="HH:mm"
								value-format = 'X' 
							/>
							<span style="font-size: 12px;">注意：清空此处则不做限制</span>
						</el-form-item>
					</el-col>
					<el-col :xs="24" class="mb20">
						<el-form-item label="当天积分上限">
							<el-input-number :min="0" v-model="state.ruleForm.pay" :controls="false" placeholder="请输入当天积分上限(0则不设上限)" class="w100" />
							<span style="font-size: 12px;">注意：此处为0则不设上限</span>
						</el-form-item>
					</el-col>
					<el-col :xs="24" class="mb20">
						<el-form-item label="单人积分上限">
							<el-input-number :value-on-clear="null" v-model="state.ruleForm.user_pay" :controls="false" placeholder="请输入单人积分上限(0则不设上限)" class="w100" />
							<span style="font-size: 12px;">注意：此处为0则不设上限</span>
						</el-form-item>
					</el-col>
					<el-col :xs="24" class="mb20">
						<el-form-item label="每日数量上限">
							<el-input-number  v-model="state.ruleForm.limits" :controls="false" placeholder="请输入每日数量上限(0则不设上限)" class="w100" />
							<span style="font-size: 12px;">注意：此处为0则不设上限</span>
						</el-form-item>
					</el-col>
					<el-col :xs="24" class="mb20">
						<el-form-item label="批量进题">
							<el-switch v-model="state.ruleForm.in_time" :active-value="1" :inactive-value="0" inline-prompt active-text="是" inactive-text="否"></el-switch>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="systemCompanyDialog">
import { reactive, ref, defineAsyncComponent } from 'vue';



// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
import { platApi } from '/@/api/plat/index';
import { ElMessage } from 'element-plus';
// 定义变量内容
const companyDialogFormRef = ref();
const state = reactive({
	qizhitimes:null,
	ruleForm: {
		time_start: 0,
		time_end: 0,
		pay: 0,
		user_pay: 0,
		limits: 0,
		in_time:1,
	},
	dialog: {
		isShowDialog: false,
		title: '',
		submitTxt: '',
	},
	plat_id:'',
});
// 打开弹窗
const openDialog = (row) => {
	state.ruleForm = {
		time_start: 0,
		time_end: 0,
		pay: 0,
		user_pay: 0,
		limits: 0,
		in_time:1,
	},
	state.plat_id = row.id;
	state.dialog.title = '修改配置';
	state.dialog.submitTxt = '修 改';
	state.dialog.isShowDialog = true;
	state.qizhitimes = null;
	platApi().conf(undefined,{id:state.plat_id,type:'plat_id'}).then(res=>{
		if(res.data){
			state.ruleForm = res.data
			state.qizhitimes = [res.data.time_start,res.data.time_end];
		}
	})
};

// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
	
};
// 提交
const onSubmit = () => {
	if(state.qizhitimes){
		state.ruleForm.time_start = state.qizhitimes[0]
		state.ruleForm.time_end = state.qizhitimes[1]
	}else{
		state.ruleForm.time_start = 0
		state.ruleForm.time_end = 0
	}
	platApi().conf(state.ruleForm,{id:state.plat_id,type:'plat_id'}).then(res=>{
		ElMessage.success('配置成功');
		closeDialog(); // 关闭弹窗
		emit('refresh');
	})
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
<style lang="scss" scoped>
	:deep(.el-input-number .el-input__inner){
		text-align: left;
	}
</style>
